#include <Display.h>
#include <TimeLib.h>
#include "TimeUtil.h"

#include "img/pangzi/i0.h"
#include "img/pangzi/i1.h"
#include "img/pangzi/i2.h"
#include "img/pangzi/i3.h"
#include "img/pangzi/i4.h"
#include "img/pangzi/i5.h"
#include "img/pangzi/i6.h"
#include "img/pangzi/i7.h"
#include "img/pangzi/i8.h"
#include "img/pangzi/i9.h"
#include "img/panda.h"
#define FONT u8g2_font_wqy16_t_gb2312b
time_t txtLoopTime = 0; // 显示文字时间

/**
 * 初始化display
 */
void Display::init(TFT_eSPI *tft_prarm, U8g2_for_TFT_eSPI *u8f_prarm)
{
    tft = tft_prarm;
    u8f = u8f_prarm;
}

/**
 * 表盘
 */
void Display::setUpClock()
{
    // //背景
    tft->fillRect(0, 36, 128, 40, TFT_WHITE);
    //时间区域（0,40：128,120）
    u8f->setFont(u8g2_font_logisoso30_tf);

    u8f->drawUTF8(5, 70, hourMinute().c_str());
    u8f->setFont(u8g2_font_tenfatguys_tu);

    u8f->drawUTF8(100, 70, num2str(second()).c_str());
    tft->drawLine(0, 120, 128, 120, TFT_BLACK);
}
/**
 * 显示天气
 */
void Display::setupWeather(WeatherData weatherData)
{
    // //背景
    tft->fillRoundRect(0, 120, 128, 160, 5, TFT_WHITE);
    //天气区域（0，0：84,40）
    u8f->setFont(FONT);
    // u8f.drawUTF8(5, 20, weatherData.weather.c_str());
    // u8f.setCursor(5,5);                // start writing at this position
    // u8f.print("天气");
    tft->drawLine(0, 30, 128, 30, TFT_BLACK);
    //地方区域（84，0：128,40）
    // u8f.drawUTF8(90, 20, weatherData.cityName.c_str());
    // tft.drawLine(84, 0, 84, 30, TFT_BLACK);
    //时间区域（0,40：128,120）
    //星期区域（0,120:38，160）

    u8f->drawUTF8(5, 145, week().c_str());
    tft->drawLine(38, 120, 38, 160, TFT_BLACK);
    //日期区域（38,120:128,160）
    u8f->drawUTF8(40, 145, monthDay().c_str());
    tft->drawLine(88, 120, 88, 160, TFT_BLACK);
    //温度区域（88,120:128,160）
    u8f->drawUTF8(93, 145, weatherData.temp.c_str());

    // PM2.5空气指数
    uint16_t pm25BgColor = tft->color565(156, 202, 127); //优
    String aqiTxt = "优";
    int pm25V = weatherData.pm25V;
    if (pm25V > 200)
    {
        pm25BgColor = tft->color565(136, 11, 32); //重度
        aqiTxt = "重度";
    }
    else if (pm25V > 150)
    {
        pm25BgColor = tft->color565(186, 55, 121); //中度x
        aqiTxt = "中度";
    }
    else if (pm25V > 100)
    {
        pm25BgColor = tft->color565(242, 159, 57); //轻
        aqiTxt = "轻度";
    }
    else if (pm25V > 50)
    {
        pm25BgColor = tft->color565(247, 219, 100); //良
        aqiTxt = "良";
    }

    // tft.fillRoundRect(10, 80, 30, 30, 5, pm25BgColor);
    // u8f.setFont(u8g2_font_fub11_tf);
    // u8f.setForegroundColor(pm25BgColor); // apply color
    // u8f.drawUTF8(15, 140, "PM:" + pm25V);

    // u8f.setForegroundColor(TFT_BLACK); // apply color
}

/**
 * 太空人图片
 */
void Display::imgAnim()
{
    int x = 80, y = 80, dt = 10; //瘦子版dt=10毫秒 胖子30较为合适

    delay(dt);
    TJpgDec.drawJpg(x, y, i1, sizeof(i1));
    delay(dt);
    TJpgDec.drawJpg(x, y, i2, sizeof(i2));
    delay(dt);
    TJpgDec.drawJpg(x, y, i3, sizeof(i3));
    delay(dt);
    TJpgDec.drawJpg(x, y, i4, sizeof(i4));
    delay(dt);
    TJpgDec.drawJpg(x, y, i5, sizeof(i5));
    delay(dt);
    TJpgDec.drawJpg(x, y, i6, sizeof(i6));
    delay(dt);
    TJpgDec.drawJpg(x, y, i7, sizeof(i7));
    delay(dt);
    TJpgDec.drawJpg(x, y, i8, sizeof(i8));
    delay(dt);
    TJpgDec.drawJpg(x, y, i9, sizeof(i9));
    delay(dt);
}

String txt = "今天天气真心不错呀";
int txtLength = 0;
int txtLoopIndex = 126;

void Display::setUpText(String txtPrams)
{
    txt = txtPrams;
    tft->fillRect(0, 0, 128, 40, TFT_WHITE);
    u8f->setFont(u8g2_font_fub11_tf);
    u8f->setForegroundColor(TFT_BLACK); // apply color
    u8f->drawUTF8(5, 20, txt.c_str());
    txtLength = u8f->getUTF8Width(txt.c_str()) + 20;
    txtLoopIndex = txtLength;
}

void Display::textLoop()
{
    u8f->setFont(FONT);
    // u8f.setForegroundColor(TFT_BLACK); // apply color
    // u8f.setBackgroundColor(TFT_WHITE); // apply color
    if (millis() - txtLoopTime > 200)
    {
        tft->fillRoundRect(0, 0, 128, 30, 5, TFT_WHITE);
        txtLoopTime = millis();
        u8f->drawUTF8(txtLoopIndex, 20, txt.c_str());

        //重置标志位
        if (txtLoopIndex > 126 - txtLength)
        {
            txtLoopIndex -= 3;
        }
        else
        {
            txtLoopIndex = txtLength;
        }
    }
}